package com.pshcp.doctor.api;

import com.framework.base.PageList;
import com.framework.base.ResultBuilder;
import com.framework.base.ResultDTO;
import com.framework.exception.BizException;
import com.framework.logaop.Log;
import com.framework.logaop.LogSourceEnum;
import com.framework.logaop.LogTypeEnum;
import com.framework.thread.ReqComTool;
import com.framework.tool.CommonTool;
import com.pshcp.doctor.ao.common.CommonAO;
import com.pshcp.doctor.ao.patient.DoctorPatientAuditPageAO;
import com.pshcp.doctor.ao.patient.DoctorPatientPoolPageAO;
import com.pshcp.doctor.ao.patient.SaveDoctorPatientRemarkAO;
import com.pshcp.doctor.tool.DoctorPatientTool;
import com.pshcp.doctor.vo.patient.DoctorArticlePatientVO;
import com.pshcp.doctor.vo.patient.DoctorPatientPoolVO;
import com.pshcp.doctor.vo.patient.DoctorPatientUngroupedVO;
import com.pshcp.doctor.vo.patient.DoctorPatientVO;
import com.pshcp.domain.dto.DoctorPatientDTO;
import com.pshcp.domain.model.DoctorArticleNotificationPatient;
import com.pshcp.domain.model.DoctorPatient;
import com.pshcp.domain.model.DoctorPatientGroup;
import com.pshcp.domain.model.DoctorPatientGroupUser;
import com.pshcp.service.DoctorArticleNotificationPatientService;
import com.pshcp.service.DoctorPatientGroupService;
import com.pshcp.service.DoctorPatientGroupUserService;
import com.pshcp.service.DoctorPatientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: TODO
 * @Author: xiaoche
 * @Date: 2022/11/4 01:34
 */
@Api(tags = {"医生端-患者管理接口"})
@Slf4j
@RestController
@RequestMapping("/api/doctor/patient")
public class DoctorPatientAPI {

    @Autowired
    private DoctorPatientService doctorPatientService;

    @Autowired
    private DoctorPatientGroupService doctorPatientGroupService;
    @Autowired
    private DoctorPatientGroupUserService doctorPatientGroupUserService;

    @Autowired
    private DoctorArticleNotificationPatientService doctorArticleNotificationPatientService;

    @ApiOperation("获取患者池列表(分页)")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "获取患者池列表(分页)")
    @PostMapping("getDoctorPatientPoolPage")
    public ResultDTO<PageList<DoctorPatientPoolVO>> getDoctorPatientPoolPage(@RequestBody DoctorPatientPoolPageAO doctorPatientPoolPageAO) throws BizException {
        DoctorPatientDTO doctorPatientDTO = DoctorPatientTool.buildGetDoctorPatientPoolPage(doctorPatientPoolPageAO);
        PageList<DoctorPatient> pageList = doctorPatientService.list(doctorPatientDTO);
        PageList<DoctorPatientPoolVO> page = CommonTool.buildPageList(pageList, DoctorPatientPoolVO.class);
        return ResultBuilder.success(page);
    }

    @ApiOperation("待审核患者列表(分页)")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "待审核患者列表(分页)")
    @PostMapping("getDoctorPatientAuditPage")
    public ResultDTO<PageList<DoctorPatientPoolVO>> getDoctorPatientAuditPage(@RequestBody DoctorPatientAuditPageAO DoctorPatientAuditPageAO) throws BizException {
        DoctorPatientDTO doctorPatientDTO = DoctorPatientTool.buildGetDoctorPatientAuditPage(DoctorPatientAuditPageAO);
        PageList<DoctorPatient> pageList = doctorPatientService.list(doctorPatientDTO);
        PageList<DoctorPatientPoolVO> page = CommonTool.buildPageList(pageList, DoctorPatientPoolVO.class);
        return ResultBuilder.success(page);
    }

    @ApiOperation("未分组患者列表(分页)")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "未分组患者列表(分页)")
    @PostMapping("getDoctorPatientUngroupedPage")
    public ResultDTO<PageList<DoctorPatientUngroupedVO>> getDoctorPatientUngroupedPage(@RequestBody DoctorPatientAuditPageAO DoctorPatientAuditPageAO) throws BizException {
        DoctorPatientDTO doctorPatientDTO = DoctorPatientTool.buildGetDoctorPatientAuditPage(DoctorPatientAuditPageAO);
        PageList<DoctorPatient> pageList = doctorPatientService.getDoctorPatientUngroupedPage(doctorPatientDTO);
        PageList<DoctorPatientUngroupedVO> page = CommonTool.buildPageList(pageList, DoctorPatientUngroupedVO.class);
        return ResultBuilder.success(page);
    }

    @ApiOperation("获取患者信息")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.UPDATE, desc = "获取患者信息")
    @PostMapping("getDoctorPatient")
    public ResultDTO<DoctorPatientVO> getDoctorPatient(@RequestBody CommonAO commonAO) throws BizException {
        CommonTool.checkObj(commonAO.getId(), "90000006");
        DoctorPatient doctorPatient = doctorPatientService.findById(commonAO.getId());
        CommonTool.checkObj(doctorPatient, "90000007");
        if (!doctorPatient.getDoctorId().equals(ReqComTool.getUserId())) {
            return ResultBuilder.failure("90000013");
        }
        DoctorPatientVO doctorPatientVO = CommonTool.copyProperties(doctorPatient, DoctorPatientVO.class);
        if (doctorPatientVO.getRelationFlag() == 0) {
            doctorPatientVO.setGroupNames("暂无分组");
        } else {
            List<DoctorPatientGroupUser> doctorPatientGroupUserList = doctorPatientGroupUserService.getDoctorPatientGroupUserList(ReqComTool.getUserId(), doctorPatient.getPatientId(), null);
            List<Long> doctorPatientGroupIds = doctorPatientGroupUserList.stream().map(DoctorPatientGroupUser::getDoctorPatientGroupId).collect(Collectors.toList());
            List<DoctorPatientGroup> doctorPatientGroups = doctorPatientGroupService.findByIds(doctorPatientGroupIds);
            List<String> groupNames = doctorPatientGroups.stream().map(DoctorPatientGroup::getName).collect(Collectors.toList());
            doctorPatientVO.setGroupNames(StringUtils.join(groupNames.toArray(), ","));
        }
        return ResultBuilder.success(doctorPatientVO);
    }

    @ApiOperation("审核患者(接受)")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.UPDATE, desc = "审核患者(接受)")
    @PostMapping("auditDoctorPatient")
    public ResultDTO<Long> auditDoctorPatient(@RequestBody CommonAO commonAO) throws BizException {
        CommonTool.checkObj(commonAO.getId(), "90000006");
        DoctorPatient doctorPatient = doctorPatientService.findById(commonAO.getId());
        CommonTool.checkObj(doctorPatient, "90000007");
        if (!doctorPatient.getDoctorId().equals(ReqComTool.getUserId())) {
            return ResultBuilder.failure("90000013");
        }
        DoctorPatient auditDoctorPatient = DoctorPatientTool.buildAuditDoctorPatient(doctorPatient);
        doctorPatientService.update(auditDoctorPatient);
        return ResultBuilder.success(null);
    }

    @ApiOperation("解除患者关系")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.UPDATE, desc = "解除患者关系")
    @PostMapping("removeDoctorPatient")
    public ResultDTO<Long> removeDoctorPatient(@RequestBody CommonAO commonAO) throws BizException {
        CommonTool.checkObj(commonAO.getId(), "90000006");
        DoctorPatient doctorPatient = doctorPatientService.findById(commonAO.getId());
        CommonTool.checkObj(doctorPatient, "90000007");
        if (!doctorPatient.getDoctorId().equals(ReqComTool.getUserId())) {
            return ResultBuilder.failure("90000013");
        }
        DoctorPatient auditDoctorPatient = DoctorPatientTool.buildRemoveDoctorPatient(doctorPatient);
        doctorPatientService.update(auditDoctorPatient);
        return ResultBuilder.success(null);
    }

    @ApiOperation("保存患者备注")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.UPDATE, desc = "保存患者备注")
    @PostMapping("saveDoctorPatientRemark")
    public ResultDTO<Long> saveDoctorPatientRemark(@RequestBody SaveDoctorPatientRemarkAO saveDoctorPatientRemarkAO) throws BizException {
        CommonTool.checkObj(saveDoctorPatientRemarkAO.getId(), "90000006");
        DoctorPatient doctorPatient = doctorPatientService.findById(saveDoctorPatientRemarkAO.getId());
        CommonTool.checkObj(doctorPatient, "90000007");
        if (!doctorPatient.getDoctorId().equals(ReqComTool.getUserId())) {
            return ResultBuilder.failure("90000013");
        }
        DoctorPatient updateDoctorPatient = DoctorPatientTool.buildSaveDoctorPatientRemark(doctorPatient, saveDoctorPatientRemarkAO.getRemark());
        doctorPatientService.update(updateDoctorPatient);
        return ResultBuilder.success(null);
    }

    @ApiOperation("获取患者列表(患教中心-提醒谁看)")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "获取患者列表(患教中心-提醒谁看)")
    @PostMapping("getDoctorArticlePatientList")
    public ResultDTO<List<DoctorArticlePatientVO>> getDoctorArticlePatientList(@RequestBody CommonAO commonAO) throws BizException {
        CommonTool.checkObj(commonAO.getId(), "90000006");
        List<DoctorPatient> doctorPatientList = doctorPatientService.getDoctorPatientList(ReqComTool.getUserId());
        List<DoctorArticleNotificationPatient> doctorArticleNotificationPatientList = doctorArticleNotificationPatientService.getDoctorArticleNotificationPatientList(commonAO.getId());
        List<DoctorArticlePatientVO> list = DoctorPatientTool.buildGetDoctorArticlePatientList(doctorPatientList, doctorArticleNotificationPatientList);
        return ResultBuilder.success(list);
    }
}
